"""
应用配置文件
包含所有应用级别的配置项
"""

from pydantic_settings import BaseSettings
from pydantic import Field, field_validator
from pydantic_settings import SettingsConfigDict


class Settings(BaseSettings):
    """应用配置类"""

    model_config = SettingsConfigDict(
        env_file=".env",
        case_sensitive=False,  # 设置不区分大小写
        extra="allow"
    )

    # 应用基本信息
    app_name: str = Field(description="应用名称")
    app_version: str = Field(description="应用版本")
    debug: bool = Field(description="调试模式")

    # API 配置
    api_v1_prefix: str = Field(description="API v1 前缀")

    # 数据库配置
    db_host: str = Field(description="数据库主机")
    db_port: int = Field(description="数据库端口")
    db_user: str = Field(description="数据库用户名")
    db_password: str = Field(description="数据库密码")
    db_name: str = Field(description="数据库名称")

    # JWT 配置
    jwt_secret_key: str = Field(description="JWT 密钥")
    jwt_algorithm: str = Field(description="JWT 算法")
    jwt_access_token_expire_minutes: int = Field(description="JWT 访问令牌过期时间(分钟)")

    # CORS 配置
    allowed_origins: list[str] = Field(description="允许的跨域源列表")

    @field_validator('allowed_origins', mode='before')
    @classmethod
    def parse_allowed_origins(cls, v):
        """解析逗号分隔的允许源列表"""
        if isinstance(v, str):
            return [origin.strip() for origin in v.split(',') if origin.strip()]
        return v


# 创建全局设置实例
settings = Settings()  # type: ignore

# JWT 相关配置（保持向后兼容）
JWT_SECRET_KEY = settings.jwt_secret_key
JWT_ALGORITHM = settings.jwt_algorithm
JWT_ACCESS_TOKEN_EXPIRE_MINUTES = settings.jwt_access_token_expire_minutes
